/**
 * @author Mie Plougstrup, Anders Abildgaard, Bo Stokholm 
 * @Version 16-12-2014
 */

package tests;

import static org.junit.Assert.fail;

import java.util.ArrayList;

import modelLayer.ChartecCode;
import modelLayer.Disposition;
import modelLayer.Driver;
import modelLayer.Trailer;

import org.junit.Test;

import dbLayer.DbBooking;
import dbLayer.DbDisposition;

public class TestDbDisposition {
	DbDisposition dbDisp = new DbDisposition();
	DbBooking dbBooking = new DbBooking();


	/**
	 * Test method for {@link dbLayer.DbDisposition#retrieveById(int, boolean)}.
	 */
	@Test
	public void testRetrieveById() {
		Disposition disp = dbDisp.retrieveById(4, true);
		if(disp != null) {
			System.out.println("Disp info:" +
					"\nid: " + disp.getDispId() +
					"\nchartecNo: " + disp.getChartecNo() +
					"\nchartecCode: " + disp.getChartecCode().getChartecCode() + " " + disp.getChartecCode().getShippingPlace() +
					"\ndriver: " + disp.getDriver().getDriverId() + " " + disp.getDriver().getfName() +
					"\ntrailer: " + disp.getTrailer().getTrailerId() + " " + disp.getTrailer().getTrailerName() +
					"\nSucces!\n");
		} else {
			fail("Disposition retrieval failed\n");	
		}
	}

	/**
	 * Test method for {@link dbLayer.DbDisposition#retrieveByDispList(int, boolean)}.
	 */
	@Test
	public void testRetrieveByDispList() {
		ArrayList<Disposition> list = dbDisp.retrieveByDispList(1, false);
		if(list != null) {
			System.out.println("Succes! Number of dispositons: " + list.size() + "\n");
		} else {
			fail("Retrival of dispositions failed\n");
		}
	}

	/**
	 * Test method for {@link dbLayer.DbDisposition#insertIntoDisposition(modelLayer.Disposition, int)}.
	 */
	@Test
	public void testInsertAndDelete() {
		Disposition disp = new Disposition(78787878, new Driver(5), new Trailer(3), new ChartecCode(8264), null);
		disp.setDispId(1000);
		try {
			dbDisp.insertIntoDisposition(disp, 1);
		} catch (Exception e) {
			e.printStackTrace();
		}
		if(dbDisp.retrieveById(1000, false) != null) {
			System.out.println("Disposition data succesfully inserted in database");
			try {
				dbDisp.insertIntoBookingSequence(disp.getDispId(), 1, 6);
			} catch (Exception e) {
				e.printStackTrace();
			}
			if(dbDisp.retrieveById(disp.getDispId(), true).getBookings().containsKey(1) == true) {
				System.out.println("BookingSequence data succesfully inserted in database");
			} else {
				fail("Insertion into BookingSequence failed");
			}
		} else {
			fail("Insertion into Disposition failed");	
		}
		
		try {
			dbDisp.deleteFromDisposition(disp);
		} catch (Exception e) {
			e.printStackTrace();
		}
		if(dbDisp.retrieveById(1000, false) == null) {
			System.out.println("Deletion was successfull!\n");
		} else {
			fail("Deletion failed\n");
		}
	}


	/**
	 * Test method for {@link dbLayer.DbDisposition#update(modelLayer.Disposition, int)}.
	 */
	@Test
	public void testUpdate() {
		Disposition disp = new Disposition(78787878, new Driver(5), new Trailer(3), new ChartecCode(8264), null);
		disp.setDispId(1000);
		try {
			dbDisp.insertIntoDisposition(disp, 1);
			System.out.println("Disp inserted");
		} catch (Exception e) {
			e.printStackTrace();
		}
		try {
			dbDisp.insertIntoBookingSequence(1000, 1, 6);
			dbDisp.insertIntoBookingSequence(1000, 2, 4);
			dbDisp.insertIntoBookingSequence(1000, 3, 5);
			System.out.println("BookingSequences inserted");
		} catch (Exception e) {
			e.printStackTrace();
		}
		Disposition changeDisp = dbDisp.retrieveById(1000, true);
		System.out.println("Disp retrieved:\n"
				+ "Bookings: " + changeDisp.getBookings().size());
		changeDisp.setChartecCode(new ChartecCode(8261));
		changeDisp.removeBooking(1);
		System.out.println("Disp changed");
		try {
			dbDisp.update(changeDisp, 1);
			System.out.println("Disp update is runned");
		} catch (Exception e) {
			e.printStackTrace();
		}
		Disposition updatedDisp = dbDisp.retrieveById(1000, true);
		System.out.println("Disp is retrieved\n"
				+ "Bookings: " + updatedDisp.getBookings().size());
	
		if(updatedDisp.getChartecCode().getChartecCode() == 8261 && updatedDisp.getBookings().size() == 2) {
			System.out.println("Updating was succesfull\n"
					+ "chartecCode: " + updatedDisp.getChartecCode().getChartecCode());
		} else {
			fail("Updating failed");
		}
		try {
			dbDisp.deleteFromDisposition(disp);
			System.out.println("Data deleted\n");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
